var util = require('../utils/util.js');
var app = getApp();
Page({
    data: {
        login: {
            account:'',
            password:''
        },
        openId:'',
        userInfo:{},
        creating: false,
        button: {
            txt: '签到'
        },
        modalHidden: true
    },
    bindPickerChange: function (e) {
        this.setData({
            'index': e.detail.value,
            'task.project_name': e.detail.value
        });
    },

    bindKeyInputAccount: function (e) {
        this.setData({
            'login.account': e.detail.value
        });
    },
    bindKeyInputPwd: function (e) {
        this.setData({
            'login.password': e.detail.value
        });
    },
    getLocation: function () {
        var that = this;
        wx.getLocation({
            type: 'gcj02', // 默认为 wgs84 返回 gps 坐标，gcj02 返回可用于 wx.openLocation 的坐标
            success: function (res) {
                console.log("定位成功");
                var locationString = res.latitude + "," + res.longitude;   //获取的经度纬度
                wx.request({
                    url: 'https://xcx.ahdhst.cn/tp/public/index.php/Index/sign/getLocation',  //根据经纬度转换成具体地址
                    data: {
                        "key": app.globalData.key,
                        "location": locationString
                    },
                    method: 'POST',
                    header: {
                        "Content-Type": "application/json"
                    },
                    // header: {},
                    success: function (res) {
                        // success
                        that.setData({
                            'task.address': res.data.result.address,
                        })
                        console.log("请求数据:" + res.data.result.address);
                    },
                    fail: function () {
                        // fail
                        console.log("请求失败");
                    },
                    complete: function () {
                        // complete
                        console.log("请求完成");
                    }
                })
            },
            fail: function () {
                // fail
                console.log("定位失败");
            },
            complete: function () {
                // complete
                console.log("定位完成");
            }
        })
    },
    // 设置任务地点
    chooseLocation: function () {
        var that = this;

        wx.chooseLocation({
            success: function (res) {
                that.setData({
                    'task.address': res.address,
                    'task.latitude': res.latitude,
                    'task.longitude': res.longitude
                })
            },
            fail: function () {
                // fail
            },
            complete: function () {
                // complete
            }
        })
    },

    // 设置打卡时间
    setSignTime: function (e) {
        var that = this;
        var hour = ((+e.detail.value.slice(0, 2) + 24 - 2) % 24).toString();
        that.setData({
            'task.signTime': e.detail.value,
            'task.signEarlyTime': (hour[1] ? hour : '0' + hour) + ':' + e.detail.value.slice(3, 5)
        });
    },

    // 设置开始日期
    startDateChange: function (e) {
        this.setData({
            'task.startDay': e.detail.value
        })
    },

    // 设置结束日期
    endDateChange: function (e) {
        this.setData({
            'task.endDay': e.detail.value
        })
    },

    // 设置重复日
    changeMonday: function (e) {
        var state = this.data.task.repeat.monday;
        this.setData({
            'task.repeat.monday': (state == 1 ? 0 : 1)
        });
    },
    changeTuesday: function (e) {
        var state = this.data.task.repeat.tuesday;
        this.setData({
            'task.repeat.tuesday': (state == 1 ? 0 : 1)
        });
    },
    changeWednesday: function (e) {
        var state = this.data.task.repeat.wednesday;
        this.setData({
            'task.repeat.wednesday': (state == 1 ? 0 : 1)
        });
    },
    changeThursday: function (e) {
        var state = this.data.task.repeat.thursday;
        this.setData({
            'task.repeat.thursday': (state == 1 ? 0 : 1)
        });
    },
    changeFriday: function (e) {
        var state = this.data.task.repeat.friday;
        this.setData({
            'task.repeat.friday': (state == 1 ? 0 : 1)
        });
    },
    changeSaturday: function (e) {
        var state = this.data.task.repeat.saturday;
        this.setData({
            'task.repeat.saturday': (state == 1 ? 0 : 1)
        });
    },
    changeSunday: function (e) {
        var state = this.data.task.repeat.sunday;
        this.setData({
            'task.repeat.sunday': (state == 1 ? 0 : 1)
        });
    },

    // 隐藏提示弹层
    modalChange: function (e) {
        this.setData({
            modalHidden: true
        })
    },
    // 提交、检验
    bindSubmit: function (e) {
        /*var that = this;
         var task = this.data.task;
         var creating = this.data.creating;

         if (task.name == '' || task.address == '点击选择地点') {
         this.setData({
         modalHidden: false
         });
         } else {
         if (!creating) {
         this.setData({
         'creating': true
         });
         that.createTask();
         }
         }*/
        var that = this;
        var task = this.data.task;
        var login = this.data.login;
        var openId = wx.getStorageSync('openId');
        var now = new Date();
        wx.request({
            url: 'https://xcx.ahdhst.cn/tp/public/index.php/Index/sign/login',
            data: {
                "key": app.globalData.key,
                PhoneNo: login.account,
                Password: login.password,
                OpenId:openId,
                token: '31:45:CB:87:6B:A5:8F:4A:D7:09:06:36:A8:EC:2C:AA:23:A7:8E:8A'
            },
            method: 'POST', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
            header: {
                "Content-Type": "application/x-www-form-urlencoded"
            }, // 设置请求的 header
            success: function (res) {
                //wx.hideToast();
                if(res.data.code=='3'){
                    wx.showToast({
                        title: 'token值不正确',
                        icon: 'loading',
                    });
                } else if(res.data.code=='2'){
                    wx.showToast({
                        title: '参数不正确',
                        icon: 'loading',
                    });
                } else if(res.data.code=='-1'){
                    wx.showToast({
                        title: '登陆失败',
                        icon: 'loading',
                    });
                } else {
                    wx.showToast({
                        title: '登录成功',
                        icon: 'loading',
                    });
                    wx.redirectTo({
                        url: '/pages/public/public',
                        success: function (res) {

                        },
                        fail: function () {
                            // fail
                        },
                        complete: function () {
                            // complete
                        }
                    })
                }
            },
            fail: function () {
                // fail
            },
            complete: function () {
                // complete
            }
        })
    },

    onShow: function () {

    },

    onHide: function () {
    },

    announce: function () {
        wx.navigateTo({
            url: '/pages/detail/announce',
        })
    },

    // 初始化设置
    onLoad: function () {
        var that = this;
        var openId = this.data.openId;
        var userInfo = this.data.userInfo;
        app.getUserInfo(function (userInfo) {
            //更新数据
            that.setData({
                userInfo: userInfo
            });

            that.setData({
                openId: openId
            });
        });
        // 初始化昵称
    }
});